<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="iOS绘图与动画系列 贝塞尔曲线UIBezierPath（二）"><meta name="keywords" content="iOS,动画,绘图"><meta name="author" content="kyren"><meta name="copyright" content="kyren"><title>iOS绘图与动画系列 贝塞尔曲线UIBezierPath（二） | Kyren's Blog</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cf87fcdbd9ab7cf93d894f7c219ae78f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script src="https://v1.hitokoto.cn/?encode=js&amp;charset=utf-8&amp;select=.footer_custom_text" defer></script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '6.2.0'
} </script><meta name="generator" content="Hexo 6.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://avatars.githubusercontent.com/u/5827823?v=4"></div><div class="author-info__name text-center">kyren</div><div class="author-info__description text-center">huluo的技术博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://github.com/huluo666">Follow Me</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">239</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">41</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">29</span></a></div><hr><div class="author-info-links"><div class="author-info-links__title text-center">Links</div><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:9090/">Jira</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://192.168.2.212:8090/">Confluence</a><a class="author-info-links__name text-center" target="_blank" rel="noopener" href="http://bloc.leniugame.com/">Leniu</a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">Kyren's Blog</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">主页</a><a class="site-page" href="/essay">随笔</a><a class="site-page" href="/archives">归档</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a><a class="site-page" href="/about">关于</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">iOS绘图与动画系列 贝塞尔曲线UIBezierPath（二）</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2016-04-08</time></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p><strong>UIBezierPath简介</strong></p>
<p>UIBezierPath类是Core Graphics框架关于path的一个封装。可以定义简单的形状，如椭圆或者矩形，或者有多个直线和曲线段组成的形状。</p>
<p><strong>为什么要用UIBezierPath绘图。。？</strong></p>
<p>上文主要介绍了C语言的绘图方式，如果使用上文所说的C语言方式绘图代码就比较多，使用<code>贝塞尔路径</code>来实现绘图，使用起来比较方便,易阅读 ,善于管理,不像上面那种阅读起来比较麻烦，相对于C语言的Core Graphics来说更为平易近人。它能够使用ARC,所以今后绘图建议使用BezierPath。</p>
<p><strong>矩形圆角设置</strong>（常用用于<code>UIIView</code>,<code>UIImageView</code>圆角设置）</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">typedef <span class="type">NS_OPTIONS</span>(<span class="type">NSUInteger</span>, <span class="type">UIRectCorner</span>) &#123;</span><br><span class="line">    <span class="type">UIRectCornerTopLeft</span>     <span class="operator">=</span> <span class="number">1</span> <span class="operator">&lt;&lt;</span> <span class="number">0</span>,<span class="comment">//左上角圆角</span></span><br><span class="line">    <span class="type">UIRectCornerTopRight</span>    <span class="operator">=</span> <span class="number">1</span> <span class="operator">&lt;&lt;</span> <span class="number">1</span>,<span class="comment">//右上角圆角</span></span><br><span class="line">    <span class="type">UIRectCornerBottomLeft</span>  <span class="operator">=</span> <span class="number">1</span> <span class="operator">&lt;&lt;</span> <span class="number">2</span>,<span class="comment">//左下角圆角</span></span><br><span class="line">    <span class="type">UIRectCornerBottomRight</span> <span class="operator">=</span> <span class="number">1</span> <span class="operator">&lt;&lt;</span> <span class="number">3</span>,<span class="comment">//右下角圆角</span></span><br><span class="line">    <span class="type">UIRectCornerAllCorners</span>  <span class="operator">=</span> <span class="operator">~</span>0UL	 <span class="comment">//全部圆角</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<p><strong>UIBezierPath对象对象创建方法</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建基本路径</span></span><br><span class="line"><span class="operator">+</span> (instancetype)bezierPath;</span><br><span class="line"><span class="comment">// 创建矩形路径</span></span><br><span class="line"><span class="operator">+</span> (instancetype)bezierPathWithRect:(<span class="type">CGRect</span>)rect;</span><br><span class="line"><span class="comment">// 创建椭圆路径</span></span><br><span class="line"><span class="operator">+</span> (instancetype)bezierPathWithOvalInRect:(<span class="type">CGRect</span>)rect;</span><br><span class="line"><span class="comment">// 创建圆角矩形</span></span><br><span class="line"><span class="operator">+</span> (instancetype)bezierPathWithRoundedRect:(<span class="type">CGRect</span>)rect cornerRadius:(<span class="type">CGFloat</span>)cornerRadius; <span class="comment">// rounds all corners with the same horizontal and vertical radius</span></span><br><span class="line"><span class="comment">// 创建指定位置圆角的矩形路径</span></span><br><span class="line"><span class="operator">+</span> (instancetype)bezierPathWithRoundedRect:(<span class="type">CGRect</span>)rect byRoundingCorners:(<span class="type">UIRectCorner</span>)corners cornerRadii:(<span class="type">CGSize</span>)cornerRadii;</span><br><span class="line"><span class="comment">// 创建弧线路径</span></span><br><span class="line"><span class="operator">+</span> (instancetype)bezierPathWithArcCenter:(<span class="type">CGPoint</span>)center radius:(<span class="type">CGFloat</span>)radius startAngle:(<span class="type">CGFloat</span>)startAngle endAngle:(<span class="type">CGFloat</span>)endAngle clockwise:(<span class="type">BOOL</span>)clockwise;</span><br><span class="line"><span class="comment">// 通过CGPath创建</span></span><br><span class="line"><span class="operator">+</span> (instancetype)bezierPathWithCGPath:(<span class="type">CGPathRef</span>)<span class="type">CGPath</span>;</span><br></pre></td></tr></table></figure>



<p><strong>相关属性</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 与之对应的CGPath</span></span><br><span class="line"><span class="meta">@property</span>(nonatomic) <span class="type">CGPathRef</span> <span class="type">CGPath</span>;</span><br><span class="line"><span class="operator">-</span> (<span class="type">CGPathRef</span>)<span class="type">CGPath</span> <span class="type">NS_RETURNS_INNER_POINTER</span> <span class="type">CF_RETURNS_NOT_RETAINED</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 是否为空，该值指示路径是否有任何有效的元素。</span></span><br><span class="line"><span class="meta">@property</span>(readonly,getter<span class="operator">=</span>isEmpty) <span class="type">BOOL</span> empty;</span><br><span class="line"><span class="comment">// 整个路径相对于原点的位置及宽高</span></span><br><span class="line"><span class="meta">@property</span>(nonatomic,readonly) <span class="type">CGRect</span> bounds;图形路径中的当前点</span><br><span class="line"><span class="comment">// 图形路径中的当前位置</span></span><br><span class="line"><span class="meta">@property</span>(nonatomic,readonly) <span class="type">CGPoint</span> currentPoint;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 线宽</span></span><br><span class="line"><span class="meta">@property</span>(nonatomic) <span class="type">CGFloat</span> lineWidth;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 终点连接类型</span></span><br><span class="line"><span class="meta">@property</span>(nonatomic) <span class="type">CGLineCap</span> lineCapStyle;</span><br><span class="line">typedef <span class="type">CF_ENUM</span>(int32_t, <span class="type">CGLineCap</span>) &#123;</span><br><span class="line">    kCGLineCapButt,<span class="comment">//斜角连接</span></span><br><span class="line">    kCGLineCapRound,<span class="comment">//圆滑衔接</span></span><br><span class="line">    kCGLineCapSquare<span class="comment">//斜角连接</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 交叉点的类型</span></span><br><span class="line"><span class="meta">@property</span>(nonatomic) <span class="type">CGLineJoin</span> lineJoinStyle;</span><br><span class="line">typedef <span class="type">CF_ENUM</span>(int32_t, <span class="type">CGLineJoin</span>) &#123;</span><br><span class="line">    kCGLineJoinMiter,</span><br><span class="line">    kCGLineJoinRound,</span><br><span class="line">    kCGLineJoinBevel</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 两条线交汇处内角和外角之间的最大距离,需要交叉点类型为kCGLineJoinMiter是生效，最大限制为10,这个限制值有助于避免在连接线段志坚的尖峰</span></span><br><span class="line"><span class="meta">@property</span>(nonatomic) <span class="type">CGFloat</span> miterLimit;</span><br><span class="line"><span class="comment">// 个人理解为绘线的精细程度，默认为0.6，数值越大，需要处理的时间越长</span></span><br><span class="line"><span class="meta">@property</span>(nonatomic) <span class="type">CGFloat</span> flatness;</span><br><span class="line"><span class="comment">// 决定使用even-odd或者non-zero规则</span></span><br><span class="line"><span class="meta">@property</span>(nonatomic) <span class="type">BOOL</span> usesEvenOddFillRule;</span><br></pre></td></tr></table></figure>





<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置画笔起始点</span></span><br><span class="line"><span class="operator">-</span> (void)moveToPoint:(<span class="type">CGPoint</span>)point;</span><br><span class="line"><span class="comment">//从当前点到指定点绘制直线</span></span><br><span class="line"><span class="operator">-</span> (void)addLineToPoint:(<span class="type">CGPoint</span>)point;</span><br><span class="line"></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 添加贝塞尔曲线</span></span><br><span class="line"><span class="comment">//endPoint终点 controlPoint1、controlPoint2控制点</span></span><br><span class="line"><span class="operator">-</span> (void)addCurveToPoint:(<span class="type">CGPoint</span>)endPoint controlPoint1:(<span class="type">CGPoint</span>)controlPoint1 controlPoint2:(<span class="type">CGPoint</span>)controlPoint2;</span><br><span class="line"><span class="comment">//endPoint终点 controlPoint控制点</span></span><br><span class="line"><span class="operator">-</span> (void)addQuadCurveToPoint:(<span class="type">CGPoint</span>)endPoint controlPoint:(<span class="type">CGPoint</span>)controlPoint;</span><br><span class="line"></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 添加弧线</span></span><br><span class="line"><span class="comment">// center弧线圆心坐标 radius弧线半径 startAngle弧线起始角度 endAngle弧线结束角度 clockwise是否顺时针绘制</span></span><br><span class="line"><span class="operator">-</span> (void)addArcWithCenter:(<span class="type">CGPoint</span>)center radius:(<span class="type">CGFloat</span>)radius startAngle:(<span class="type">CGFloat</span>)startAngle endAngle:(<span class="type">CGFloat</span>)endAngle clockwise:(<span class="type">BOOL</span>)clockwise <span class="type">NS_AVAILABLE_IOS</span>(<span class="number">4_0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//闭合路径，封闭未形成闭环的路径</span></span><br><span class="line"><span class="operator">-</span> (void)closePath;</span><br><span class="line"></span><br><span class="line"><span class="comment">//移除所有的点，删除所有的subPath</span></span><br><span class="line"><span class="operator">-</span> (void)removeAllPoints;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 追加bezierPath路径</span></span><br><span class="line"><span class="operator">-</span> (void)appendPath:(<span class="type">UIBezierPath</span> <span class="operator">*</span>)bezierPath;</span><br><span class="line"><span class="comment">// 创建并返回一个新的反转内容的当前路径曲线路径的对象。</span></span><br><span class="line"><span class="operator">-</span> (<span class="type">UIBezierPath</span> <span class="operator">*</span>)bezierPathByReversingPath <span class="type">NS_AVAILABLE_IOS</span>(<span class="number">6_0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 用指定的仿射变换矩阵变换路径的所有点</span></span><br><span class="line"><span class="operator">-</span> (void)applyTransform:(<span class="type">CGAffineTransform</span>)transform;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="operator">-</span> (void)fill;		<span class="comment">//填充</span></span><br><span class="line"><span class="operator">-</span> (void)stroke;		<span class="comment">//路径绘制</span></span><br><span class="line"><span class="operator">-</span> (void)addClip;	<span class="comment">//在这以后的图形绘制超出当前路径范围则不可见</span></span><br></pre></td></tr></table></figure>



<p><strong>画图示例代码</strong></p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 直线</span></span><br><span class="line"><span class="operator">-</span> (void)drawRect2:(<span class="type">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 设置线的填充色</span></span><br><span class="line">    [[<span class="type">UIColor</span> redColor] setStroke];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 新建一个bezier对象</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPath];</span><br><span class="line">    <span class="comment">// 设置线宽度</span></span><br><span class="line">    bezierPath.lineWidth <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 设置线两头样式</span></span><br><span class="line">    bezierPath.lineCapStyle <span class="operator">=</span> kCGLineCapRound;</span><br><span class="line">    <span class="comment">// 设置起点、终点坐标</span></span><br><span class="line">    [bezierPath moveToPoint:<span class="type">CGPointMake</span>(<span class="number">10</span>, <span class="number">10</span>)];</span><br><span class="line">    [bezierPath addLineToPoint:<span class="type">CGPointMake</span>(<span class="number">100</span>, <span class="number">100</span>)];</span><br><span class="line">    <span class="comment">// 开始绘制</span></span><br><span class="line">    [bezierPath stroke];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 二阶曲线</span></span><br><span class="line"><span class="operator">-</span> (void)drawRect3:(<span class="type">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 设置线的填充色</span></span><br><span class="line">    [[<span class="type">UIColor</span> redColor] setStroke];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 新建一个bezier对象</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPath];</span><br><span class="line">    <span class="comment">// 设置线宽度</span></span><br><span class="line">    bezierPath.lineWidth <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 设置线两头样式</span></span><br><span class="line">    bezierPath.lineCapStyle <span class="operator">=</span> kCGLineCapRound;</span><br><span class="line">    <span class="comment">// 设置起点、终点坐标</span></span><br><span class="line">    [bezierPath moveToPoint:<span class="type">CGPointMake</span>(<span class="number">100</span>, <span class="number">100</span>)];</span><br><span class="line">    [bezierPath addQuadCurveToPoint:<span class="type">CGPointMake</span>(<span class="number">200</span>, <span class="number">200</span>) controlPoint:<span class="type">CGPointMake</span>(<span class="number">300</span>, <span class="number">0</span>)];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 开始绘制</span></span><br><span class="line">    [bezierPath stroke];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 三阶曲线</span></span><br><span class="line"><span class="operator">-</span> (void)drawRect4:(<span class="type">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 设置线的填充色</span></span><br><span class="line">    [[<span class="type">UIColor</span> redColor] setStroke];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 新建一个bezier对象</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPath];</span><br><span class="line">    <span class="comment">// 设置线宽度</span></span><br><span class="line">    bezierPath.lineWidth <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 设置线两头样式</span></span><br><span class="line">    bezierPath.lineCapStyle <span class="operator">=</span> kCGLineCapRound;</span><br><span class="line">    <span class="comment">// 设置起点、终点坐标</span></span><br><span class="line">    [bezierPath moveToPoint:<span class="type">CGPointMake</span>(<span class="number">100</span>, <span class="number">100</span>)];</span><br><span class="line">    [bezierPath addCurveToPoint:<span class="type">CGPointMake</span>(<span class="number">200</span>, <span class="number">200</span>) controlPoint1:<span class="type">CGPointMake</span>(<span class="number">150</span>, <span class="number">0</span>) controlPoint2:<span class="type">CGPointMake</span>(<span class="number">150</span>, <span class="number">300</span>)];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 开始绘制</span></span><br><span class="line">    [bezierPath stroke];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 多阶曲线</span></span><br><span class="line"><span class="operator">-</span> (void)drawRect5:(<span class="type">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 设置线的填充色</span></span><br><span class="line">    [[<span class="type">UIColor</span> redColor] setStroke];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 新建一个bezier对象</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPath];</span><br><span class="line">    <span class="comment">// 设置线宽度</span></span><br><span class="line">    bezierPath.lineWidth <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 设置线两头样式</span></span><br><span class="line">    bezierPath.lineCapStyle <span class="operator">=</span> kCGLineCapRound;</span><br><span class="line">    <span class="comment">// 设置起点、终点坐标</span></span><br><span class="line">    [bezierPath moveToPoint:<span class="type">CGPointMake</span>(<span class="number">100</span>, <span class="number">100</span>)];</span><br><span class="line">    [bezierPath addCurveToPoint:<span class="type">CGPointMake</span>(<span class="number">200</span>, <span class="number">200</span>)</span><br><span class="line">                  controlPoint1:<span class="type">CGPointMake</span>(<span class="number">150</span>, <span class="number">0</span>)</span><br><span class="line">                  controlPoint2:<span class="type">CGPointMake</span>(<span class="number">150</span>, <span class="number">300</span>)];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建第二条贝塞尔曲线</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath2 <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPath];</span><br><span class="line">    <span class="comment">// 设置起点、终点坐标</span></span><br><span class="line">    [bezierPath2 moveToPoint:<span class="type">CGPointMake</span>(<span class="number">200</span>, <span class="number">200</span>)];</span><br><span class="line">    [bezierPath2 addCurveToPoint:<span class="type">CGPointMake</span>(<span class="number">290</span>, <span class="number">290</span>)</span><br><span class="line">                   controlPoint1:<span class="type">CGPointMake</span>(<span class="number">250</span>, <span class="number">0</span>)</span><br><span class="line">                   controlPoint2:<span class="type">CGPointMake</span>(<span class="number">250</span>, <span class="number">300</span>)];</span><br><span class="line">    <span class="comment">// 将第二条线，加到第一条线上面去</span></span><br><span class="line">    [bezierPath appendPath:bezierPath2];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 开始绘制</span></span><br><span class="line">    [bezierPath stroke];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#pragma mark<span class="operator">---</span></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 画矩形</span></span><br><span class="line"><span class="operator">-</span> (void)drawRect10:(<span class="type">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 设置线的填充色</span></span><br><span class="line">    [[<span class="type">UIColor</span> redColor] setStroke];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 新建一个bezier对象，此对象用于绘制矩形，需要传入绘制的矩形的Frame</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPathWithRect:<span class="type">CGRectMake</span>(<span class="number">10</span>, <span class="number">10</span>, <span class="number">280</span>, <span class="number">280</span>)];</span><br><span class="line">    <span class="comment">// 设置线宽度</span></span><br><span class="line">    bezierPath.lineWidth <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 设置线两头样式</span></span><br><span class="line">    bezierPath.lineCapStyle <span class="operator">=</span> kCGLineCapRound;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 开始绘制</span></span><br><span class="line">    [bezierPath stroke];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 2:画矩形，圆角矩形</span></span><br><span class="line"><span class="operator">-</span> (void)drawRect11:(<span class="type">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 设置线的填充色</span></span><br><span class="line">    [[<span class="type">UIColor</span> redColor] setStroke];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 新建一个bezier对象，此对象用于绘制一个圆角矩形</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPathWithRoundedRect:<span class="type">CGRectMake</span>(<span class="number">10</span>, <span class="number">10</span>, <span class="number">280</span>, <span class="number">280</span>)</span><br><span class="line">                                                          cornerRadius:<span class="number">30</span>];</span><br><span class="line">    <span class="comment">// 设置线宽度</span></span><br><span class="line">    bezierPath.lineWidth <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 开始绘制</span></span><br><span class="line">    [bezierPath stroke];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  rect: 需要画的矩形的Frame</span></span><br><span class="line"><span class="comment"> *  corners: 哪些部位需要画成圆角</span></span><br><span class="line"><span class="comment"> *  cornerRadii: 圆角的Size</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 画矩形，部分圆角的矩形</span></span><br><span class="line"><span class="operator">-</span> (void)drawRect12:(<span class="type">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 设置线的填充色</span></span><br><span class="line">    [[<span class="type">UIColor</span> redColor] setStroke];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 新建一个bezier对象，此对象用于绘制一个部分圆角的矩形，左上、右下</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPathWithRoundedRect:<span class="type">CGRectMake</span>(<span class="number">10</span>, <span class="number">10</span>, <span class="number">280</span>, <span class="number">280</span>)</span><br><span class="line">                                                     byRoundingCorners:<span class="type">UIRectCornerTopLeft</span> <span class="operator">|</span> <span class="type">UIRectCornerBottomRight</span></span><br><span class="line">                                                           cornerRadii:<span class="type">CGSizeMake</span>(<span class="number">10</span>, <span class="number">10</span>)];</span><br><span class="line">    <span class="comment">// 设置线宽度</span></span><br><span class="line">    bezierPath.lineWidth <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 开始绘制</span></span><br><span class="line">    [bezierPath stroke];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 画圆</span></span><br><span class="line"><span class="operator">-</span> (void)drawRect13:(<span class="type">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 设置线的填充色</span></span><br><span class="line">    [[<span class="type">UIColor</span> redColor] setStroke];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 新建一个bezier对象，此对象用于绘制内切圆，需要传入绘制内切圆的矩形的Frame</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPathWithOvalInRect:<span class="type">CGRectMake</span>(<span class="number">10</span>, <span class="number">10</span>, <span class="number">280</span>, <span class="number">280</span>)];</span><br><span class="line">    <span class="comment">// 设置线宽度</span></span><br><span class="line">    bezierPath.lineWidth <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line">    <span class="comment">// 设置线两头样式</span></span><br><span class="line">    bezierPath.lineCapStyle <span class="operator">=</span> kCGLineCapRound;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 开始绘制</span></span><br><span class="line">    [bezierPath stroke];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  center: 圆心坐标</span></span><br><span class="line"><span class="comment"> *  radius: 圆的半径</span></span><br><span class="line"><span class="comment"> *  startAngle: 绘制起始点角度</span></span><br><span class="line"><span class="comment"> *  endAngle: 绘制终点角度</span></span><br><span class="line"><span class="comment"> *  clockwise: 是否顺时针</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">//<span class="doctag">TODO:</span> 圆弧</span></span><br><span class="line"><span class="operator">-</span> (void)drawRect14:(<span class="type">CGRect</span>)rect</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 设置线的填充色</span></span><br><span class="line">    [[<span class="type">UIColor</span> redColor] setStroke];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 新建一个bezier对象，此对象用于绘制一个圆弧</span></span><br><span class="line">    <span class="type">UIBezierPath</span> <span class="operator">*</span>bezierPath <span class="operator">=</span> [<span class="type">UIBezierPath</span> bezierPathWithArcCenter:<span class="type">CGPointMake</span>(<span class="number">150</span>, <span class="number">150</span>)</span><br><span class="line">                                                              radius:<span class="number">110</span></span><br><span class="line">                                                          startAngle:<span class="number">0</span></span><br><span class="line">                                                            endAngle:<span class="type">M_PI_2</span></span><br><span class="line">                                                           clockwise:<span class="type">NO</span>];</span><br><span class="line">    <span class="comment">// 设置线宽度</span></span><br><span class="line">    bezierPath.lineWidth <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 开始绘制</span></span><br><span class="line">    [bezierPath stroke];</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p><strong><code>工具推荐</code></strong></p>
<p><a target="_blank" rel="noopener" href="https://github.com/YouXianMing/Tween-o-Matic-CN">https://github.com/YouXianMing/Tween-o-Matic-CN</a>这是个自定义贝塞尔曲线值的mac工具</p>
<p>由于图像绘制代码一般较多，下面这些工具可以 简单快速生成绘制代码，实时查看效果，简单高效，生成的代码也很规范。</p>
<p>1、<code>paintCode</code></p>
<blockquote>
<p>PaintCode是由来自斯洛伐克首都伯拉第斯拉瓦的<a target="_blank" rel="noopener" href="http://www.pixelcut.com/"><strong>PixelCut</strong></a>软件公司推出的，一款面向iOS和Mac应用开发者及设计师的矢量图形可视化开发工具。通过PaintCode，即使是没有编程经验的设计师也能绘制出精美的控件、图标及其他UI元素。而PaintCode最为显著的一点就是能够直接生成适用于iOS的objectivec或Swift的代码，节省了大量的编程时间，也正因如此，许多开发者将其称为设计与开发通吃的代码神器。</p>
</blockquote>
<p>2、<code>QuartzCode</code></p>
<p>官网：<a target="_blank" rel="noopener" href="http://www.quartzcodeapp.com/">http://www.quartzcodeapp.com/</a></p>
<blockquote>
<p>QuartzCode 是一个快速的、 轻量级的、 强大的动画工具，转换矢量绘图和动画到Object C 和 Swift 代码。我们只需更改属性 ，还可以可以循环在几秒钟内，实时看到动画的变化。减少了在 Xcode 创建动画的障碍 ！</p>
</blockquote>
<p>3、<code>Sketch</code> 	  矢量图设计工具</p>
<p><a target="_blank" rel="noopener" href="http://www.sketchcn.com/">http://www.sketchcn.com/</a></p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">kyren</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://huluo666.gitee.io/2016/04/08/iOS绘图与动画系列 贝塞尔曲线UIBezierPath（二）/">http://huluo666.gitee.io/2016/04/08/iOS绘图与动画系列 贝塞尔曲线UIBezierPath（二）/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://huluo666.gitee.io">Kyren's Blog</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/iOS-%E5%8A%A8%E7%94%BB-%E7%BB%98%E5%9B%BE/">iOS,动画,绘图</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2016/04/08/iOS%E7%BB%98%E5%9B%BE%E4%B8%8E%E5%8A%A8%E7%94%BB%E7%B3%BB%E5%88%97%20UIDynamicAnimator%EF%BC%88%E4%BA%94%EF%BC%89/"><i class="fa fa-chevron-left">  </i><span>iOS绘图与动画系列 UIDynamicAnimator（五）</span></a></div><div class="next-post pull-right"><a href="/2016/04/07/Mac%20%E5%BF%AB%E6%8D%B7%E9%94%AE/"><span>Mac 快捷键</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2022 By kyren</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="footer_custom_text">hitokoto</div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/lib/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>